package com.hanbang.maige.code.base.view.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
import com.hanbang.maige.R;
import com.hanbang.maige.code.ActivityManager;
import com.hanbang.maige.code.base.presenter.BasePresenter;
import com.hanbang.maige.code.base.view.iview.BaseView;
import com.hanbang.maige.utils.ui.DrawableUtils;
import com.hanbang.maige.utils.ui.SnackbarUtil;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/8/15.
 */

public abstract class BaseMainActivity<V extends BaseView, T extends BasePresenter<V>> extends BaseMvpActivity<V, T> implements AHBottomNavigation.OnTabSelectedListener {
    @BindView(R.id.bottom_navigation_bar)
    AHBottomNavigation ahBottomNavigation;
    private long exitTime = 0;
    private static String broadFlag = "Buttom_BroadFlag";
    private MyBroadcastReceiver broadcastReceiver = new MyBroadcastReceiver();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        IntentFilter intentFilter = new IntentFilter(broadFlag);
        registerReceiver(broadcastReceiver, intentFilter);
    }

    @Override
    public void initView() {
        ahBottomNavigation.setAccentColor(getResources().getColor(R.color.main_color));

        ahBottomNavigation.addItem(new AHBottomNavigationItem(getResources().getString(R.string.bottom_1),
                DrawableUtils.getStateSelectDrawable(R.mipmap.ic_launcher, R.mipmap.ic_launcher)));
        ahBottomNavigation.addItem(new AHBottomNavigationItem(getResources().getString(R.string.bottom_2),
                DrawableUtils.getStateSelectDrawable(R.mipmap.ic_launcher, R.mipmap.ic_launcher)));
        ahBottomNavigation.addItem(new AHBottomNavigationItem(getResources().getString(R.string.bottom_3),
                DrawableUtils.getStateSelectDrawable(R.mipmap.ic_launcher, R.mipmap.ic_launcher)));
        ahBottomNavigation.addItem(new AHBottomNavigationItem(getResources().getString(R.string.bottom_4),
                DrawableUtils.getStateSelectDrawable(R.mipmap.ic_launcher, R.mipmap.ic_launcher)));

        ahBottomNavigation.setDefaultBackgroundColor(getResources().getColor(R.color.main_color));
        ahBottomNavigation.setCurrentItem(currentItem(), false);
        ahBottomNavigation.setForceTitlesDisplay(true);
        ahBottomNavigation.setOnTabSelectedListener(this);


//        toolbar.setTitle("首页");
//        convenientBanner.setPageIndicator(new int[]{R.drawable.banner_circle_default, R.drawable.banner_circle_select});
//        convenientBanner.setPages(this, bannerAdDatas);
//
//        bannerAdDatas.add(new BannerAdData(1, "http://imgsrc.baidu.com/forum/pic/item/b1c343d9f2d3572ccf18c7308a13632763d0c373.jpg"));
//        bannerAdDatas.add(new BannerAdData(2, "http://img0.imgtn.bdimg.com/it/u=891814847,3397298689&fm=21&gp=0.jpg"));
//        bannerAdDatas.add(new BannerAdData(3, "http://img3.imgtn.bdimg.com/it/u=2286035086,4168118241&fm=21&gp=0.jpg"));
//        bannerAdDatas.add(new BannerAdData(4, "http://img4.imgtn.bdimg.com/it/u=3806360125,3446946955&fm=21&gp=0.jpg"));
//        convenientBanner.setPages(HomeActivity.this, bannerAdDatas);
//        convenientBanner.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                bannerAdDatas.clear();
//                bannerAdDatas.add(new BannerAdData(2, "http://img0.imgtn.bdimg.com/it/u=891814847,3397298689&fm=21&gp=0.jpg"));
//                bannerAdDatas.add(new BannerAdData(1, "http://imgsrc.baidu.com/forum/pic/item/b1c343d9f2d3572ccf18c7308a13632763d0c373.jpg"));
//
//                convenientBanner.notifyDataSetChanged();
//            }
//        }, 2000);

    }

    @Override
    public void parseIntent(Intent intent) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        ahBottomNavigation.setCurrentItem(currentItem(), false);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    public abstract int currentItem();

    @Override
    public boolean onTabSelected(int position, boolean wasSelected) {
        if (position != currentItem()) {
            Intent intent = new Intent(broadFlag);
            intent.putExtra("position", position);
            sendBroadcast(intent);
            if (position == 0) {
                // HomeActivity.startUI(this);
            } else if (position == 1) {
                // JianghuActivity.startUI(this);
            } else if (position == 2) {
                //JieyouActivity.startUI(this, 0);
            } else if (position == 3) {
                // ChanpingActivity.startUI(this);
            } else if (position == 4) {
                // AboutMeActivity.startUI(this);
            }
        }
        return true;
    }

    private class MyBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            ahBottomNavigation.setCurrentItem(intent.getIntExtra("position", 0), false);
        }
    }

    @Override
    public void onBackPressed() {
        isExitApplication(this);
    }


    public void isExitApplication(Context context) {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            SnackbarUtil.showLong(this, "再按一次退出程序", SnackbarUtil.Info).show();
            exitTime = System.currentTimeMillis();
        } else {
            // 退出
            ActivityManager.getInstance().exitAllActivity();
        }
    }
}
